using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyHunter.Log
{
    public partial class LogForm : Form
    {
        #region MEMBERS
        private DataSchema.CollectionSchema.LastHuntLogDataTable LogTab = null;
        private DataSchema.CollectionSchema.FinishedShotsDataTable ShotTab = null;
        private DataSchema.CollectionSchema.FinishedEntitiesDataTable EntTab = null;
        #endregion

        #region CTOR
        public LogForm(DataSchema.CollectionSchema.LastHuntLogDataTable t,
            DataSchema.CollectionSchema.FinishedShotsDataTable fs,
            DataSchema.CollectionSchema.FinishedEntitiesDataTable fe)
        {
            InitializeComponent();

            this.lastHuntLogDataTableBindingSource.DataSource = t;
            this.finishedEntitiesDataTableBindingSource.DataSource = fe;
            this.finishedShotsDataTableBindingSource.DataSource = fs;
            
            LogTab = t;
            ShotTab = fs;
            EntTab = fe;

            this.textBoxHunted.Text = Format("New");
            this.textBoxDuplicated.Text = Format("Duplicated");
            this.textBoxValid.Text = Format("Valid");
            this.textBoxError.Text = Format("Error");
        }
        #endregion

        #region PRIVATE
        private string Format(string state)
        {
            object sumObject;
            DataRow[] r = this.LogTab.Select("Info = '" + state + "'");
            sumObject = LogTab.Compute("Sum(Size)", "Info = '" + state + "'");
            long sumsize = 0;
            if(sumObject != null && sumObject!= System.DBNull.Value)
            {
                sumsize = long.Parse(sumObject.ToString());
            }
            return r.Length.ToString()+ "/" + Engine.IO.FileTools.GetSize(sumsize);
        }

        private void SelectItems (bool sel)
        {
            if(this.tabControl.SelectedTab == this.tpHuntedFiles)
            {
                foreach (DataSchema.CollectionSchema.LastHuntLogRow lr in LogTab.Rows)
                {
                    lr.Selected = sel;
                }
            }
            else if(this.tabControl.SelectedTab == this.tpFinisfedShots)
            {
                foreach (DataSchema.CollectionSchema.FinishedShotsRow lr in ShotTab.Rows)
                {
                    lr.Selected = sel;
                }
            }
            else if(this.tabControl.SelectedTab == this.tpFinishedEntities)
            {
                foreach (DataSchema.CollectionSchema.FinishedEntitiesRow lr in EntTab.Rows)
                {
                    lr.Selected = sel;
                }
            }

        }

        #endregion

        #region EVENTS
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            List<DataRow> list = new List<DataRow>();

            if(this.tabControl.SelectedTab == this.tpHuntedFiles)
            {
                foreach(DataSchema.CollectionSchema.LastHuntLogRow lr in LogTab.Rows)
                {
                    try
                    {
                        if (lr.Selected)
                        {
                            Engine.IO.FileTools.DeleteFile(lr.Path, DataSchema.Settings.SettingsMgr.RemoveEmptyFolders);
                            lr.Info = "Deleted";
                            list.Add(lr);
                        }
                    }
                    catch (Exception)//skip
                    {
                        lr.Info = "Can't remove this file or it's parent directory";
                    }
                }
            }
            else if(this.tabControl.SelectedTab == this.tpFinisfedShots)
            {
                foreach(DataSchema.CollectionSchema.FinishedShotsRow lr in ShotTab.Rows)
                {
                    try
                    {
                        if (lr.Selected)
                        {
                            list.Add(lr);
                        }
                    }
                    catch (Exception)//skip
                    { }

                }
            }
            else if(this.tabControl.SelectedTab == this.tpFinishedEntities)
            {
                foreach(DataSchema.CollectionSchema.FinishedEntitiesRow lr in EntTab.Rows)
                {
                    try
                    {
                        if (lr.Selected)
                        {
                            list.Add(lr);
                        }
                    }
                    catch (Exception)//skip
                    { }
                }
            }
            foreach(DataRow dr2r in list)
            {
                dr2r.Delete();
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            SelectItems(true);
        }

        private void buttonDeselect_Click(object sender, EventArgs e)
        {
            SelectItems(false);
        }

        #endregion
    }
}